package com.ljq.software_springboot.softwareprogram.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ljq.software_springboot.softwareprogram.entity.Account;
import com.ljq.software_springboot.softwareprogram.mapper.AccountMapper;
import com.ljq.software_springboot.softwareprogram.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-18
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements IAccountService {

    @Autowired
    private AccountMapper accountMapper;


    /**
     * 用户注册
     *
     * @param username 用户名
     * @param password 密码
     * @param email    邮箱
     * @return 注册结果
     */
    public String register(String username, String password, String email) {
        // 检查用户是否已存在
        LambdaQueryWrapper<Account> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Account::getUsername, username);
        Account existingAccount = accountMapper.selectOne(queryWrapper);
        if (existingAccount != null) {
            return "User already exists";
        }


        // 保存用户信息到数据库
        Account newAccount = new Account();
        newAccount.setUsername(username);
        newAccount.setPassword(password);
        newAccount.setEmail(email);
        accountMapper.insert(newAccount);

        return "Register success";
    }

    /**
     * 用户登录
     *
     * @param username 用户名
     * @param password 密码
     * @return 登录结果
     */
// 登录方法
    public String login(String username, String password) {
        // 查询用户
        LambdaQueryWrapper<Account> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Account::getUsername, username);
        Account account = accountMapper.selectOne(queryWrapper) ;

        // 用户不存在
        if (account == null) {
            return "没有该账户";
        }

        // 验证密码
        if (!password.equals(account.getPassword())) {
            return "账户或密码错误";
        }

        // 登录成功
        return "Login success";
    }
    public Account getUserByUsername(String username) {
        try {
            LambdaQueryWrapper<Account> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Account::getUsername, username);
            Account account = accountMapper.selectOne(queryWrapper);
            if (account != null) {
                System.out.println("查找成功");
                System.out.println(account.getUsername());
            } else {
                System.out.println("未找到该用户");
            }
            return account;
        } catch (Exception e) {
            System.out.println("查询过程中出现异常: " + e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    //修改密码
    public String changePassword(String username, String oldPassword, String newPassword) {
        try {
            LambdaQueryWrapper<Account> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Account::getUsername, username);
            Account account = accountMapper.selectOne(queryWrapper) ;
            if (account == null) {
                throw new RuntimeException("用户不存在");
            }

            // 验证当前密码
            if (!oldPassword.equals(account.getPassword())) {
                throw new RuntimeException("当前密码不正确");
            }

            // 修改密码
            account.setPassword(newPassword);
            System.out.println(account.getPassword());
            accountMapper.updateById(account);
        } catch (Exception e) {
            return "修改密码失败，请稍后再试";
        }
        return "success";
    }
    public Integer findId(String username){

        LambdaQueryWrapper<Account> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Account::getUsername, username);
        Account existingAccount = accountMapper.selectOne(queryWrapper);
        return existingAccount.getId();
    }
}
